﻿using EMRView.Extensions.HCAttribute;
using HC.View;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EMRView.Extensions.Command.StandardCommand.Edit
{
    [Command(CommandType.编辑, "EditProtect", "编辑保护")]
    public class EditProtectCommand : BaseCommand
    {
        public override Icon CommandIcon => null;

        public override object Execute(HCEmrView _view, bool showDefaultUI = false, params object[] para)
        {
            HCCustomData vTopData = _view.ActiveSectionTopLevelData();

            if (vTopData.SelectExists())
            {
                for (int i = vTopData.SelectInfo.StartItemNo; i <= vTopData.SelectInfo.EndItemNo; i++)
                {
                    if (vTopData.Items[i].StyleNo < HCStyle.Null)
                    {
                        MessageBox.Show("禁止编辑只能应用于文本内容，选中内容中存在非文本对象！");
                        return null;
                    }
                }

                if ((vTopData.SelectInfo.StartItemNo == vTopData.SelectInfo.EndItemNo)
                    && (vTopData.SelectInfo.StartItemOffset == 0)
                    && (vTopData.SelectInfo.EndItemOffset == vTopData.GetItemOffsetAfter(vTopData.SelectInfo.StartItemNo)))  // 在同一个Item
                {
                    (vTopData.Items[vTopData.SelectInfo.StartItemNo] as DeItem).EditProtect = true;
                    return null;
                }

                for (int i = vTopData.SelectInfo.StartItemNo; i <= vTopData.SelectInfo.EndItemNo; i++)
                    (vTopData.Items[i] as DeItem).EditProtect = false;

                string vS = vTopData.GetSelectText();
                vS = vS.Replace("\n", "").Replace("\t", "").Replace("\r", "");
                DeItem vDeItem = _view.NewDeItem(vS);
                vDeItem.EditProtect = true;
                _view.InsertDeItem(vDeItem);
            }
            else
            {
                DeItem vDeItem = vTopData.GetActiveItem() as DeItem;
                vDeItem.EditProtect = !vDeItem.EditProtect;
                _view.ActiveItemReAdaptEnvironment();
            }
            return null;
        }

        public override void InitControl(object control)
        {

        }
    }
}
